package com.dflc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dflc.common.CommonConstant;
import com.dflc.entity.po.UUnit;
import com.dflc.entity.vo.UUnitVo;
import com.dflc.entity.vo.UUnitVo2;
import com.dflc.mapper.UUnitMapper;
import com.dflc.service.UUnitService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 86158
* @description 针对表【u_unit(单位/责任部门)】的数据库操作Service实现
* @createDate 2025-02-21 11:07:36
*/
@Service
public class UUnitServiceImpl extends ServiceImpl<UUnitMapper, UUnit>
    implements UUnitService{

    @Resource
    private UUnitMapper uUnitMapper;

    @Override
    public List<UUnitVo> getUnitSelect(String key) {
            Map<String, Object> params4 = new HashMap<>();
            params4.put("key", key);
            params4.put("state", CommonConstant.StateActive.Active);
            List<UUnit> uUnits = uUnitMapper.findAllByMap(params4);
            List<UUnitVo> voList = new ArrayList<>();
            for (UUnit uUnit : uUnits) {
                UUnitVo vo= new UUnitVo();
                vo.setId(uUnit.getId());
                vo.setUnitAllName(uUnit.getUnitAllName());
                vo.setUnitForShort(uUnit.getUnitForShort());
                voList.add(vo);
            }
            return voList;
    }

    @Override
    public List<UUnitVo2> getAllTree(Integer node) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId",node);
        List<UUnitVo2>  list = uUnitMapper.findAllByMap2(params);
        for (UUnitVo2 treeVo : list){
                //判断是否有下级
                Map<String, Object> params2 = new HashMap<>();
                params2.put("parentId",treeVo.getId());
                List<UUnitVo2>  list2 = uUnitMapper.findAllByMap2(params2);
                if (list2.size()>0){
                    treeVo.setIsChild(true);
                }else {
                    treeVo.setIsChild(false);
                }
        }
        //把list按照sort 的升序排序
        list.sort(Comparator.comparing(UUnitVo2::getSort));
        return list;
    }

    @Override
    public List<UUnitVo2> getAllTree2(Map<String, String> params1) {
        List<UUnitVo2>  list = uUnitMapper.findAllByMap3(params1);
        List<UUnitVo2> list1 = new ArrayList<>();
        //把list集合按照parentId字段进行分组，用stream流处理
        Map<Integer, List<UUnitVo2>> map = list.stream().collect(Collectors.groupingBy(UUnitVo2::getParentId));
        //把map集合按照key的升序排序
        Map<Integer, List<UUnitVo2>> sortMap = map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        //遍历map
        for (Map.Entry<Integer, List<UUnitVo2>> entry : sortMap.entrySet()) {
            //获取key
            Integer key = entry.getKey();
            //获取value
            List<UUnitVo2> value = entry.getValue();
            if (key.intValue()==0){
                //是根
                //遍历value
                for (UUnitVo2 treeVo : value){
                    list1.add(treeVo);
                }
            }else {
                //不是根则判断list1里面有没有
                if (!ObjectUtils.isEmpty(list1) && list1.size()>0){
                  boolean a  =   findParentId(list1,key,value);
                  if (a){
                      continue;
                  }else {
                      //没有则遍历value
                      for (UUnitVo2 treeVo : value){
                          list1.add(treeVo);
                      }
                  }
                }else {
                    //没有则遍历value
                    for (UUnitVo2 treeVo : value){
                        list1.add(treeVo);
                    }
                }

            }
        }
        //把集合list1按照sort的升序排序
        list1.sort(Comparator.comparing(UUnitVo2::getSort));
        return list1;
    }


    public boolean  findParentId(List<UUnitVo2> list1,Integer key,List<UUnitVo2> value) {
        boolean f =false;
        for (UUnitVo2 treeVo : list1){
            if (treeVo.getId().intValue()==key.intValue()){
                //有则遍历value
                f=true;
                List<UUnitVo2> list3 = new ArrayList<>();
                for (UUnitVo2 treeVo2 : value){
                    list3.add(treeVo2);
                }
                treeVo.setList(list3);
                break;
            }else {
                //现在的新树list1集合的该treeVo没有匹配上，则判断是否有子集有子集则还需要询问子集
                if (!ObjectUtils.isEmpty(treeVo.getList()) && treeVo.getList().size()!=0){
                   boolean chileF =    findParentId(treeVo.getList(),key,value);
                   if (chileF){
                       f=true;
                   }else {
                       //没有子集能匹配上,则继续
                       continue;
                   }
                }else {
                    //没有子集,则继续
                    continue;
                }
            }
        }
        return f;
    }
}




